package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.baseController.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.utils.MD5DES;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.Client;
import com.dc.model.User;
import com.dc.service.ClientService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

// 标明swagger的信息 用在请求的类上，表示对类的说明
@Api(value="客户模块")
// 前缀
@RequestMapping("/client")
//标明控制类，被mapper扫描到
@Controller
public class ClientController extends BaseController {
    @Autowired
    ClientService clientService;

    //  分页查询客户信息
    //  新增操作日志
    @AopOperation(desc = "查询客户",type="查询",menu = RoleMenuEnum.NO_206)
    //  添加api注解
    @ApiOperation(value="分页查询客户信息",httpMethod = "GET",notes = "查询")
    // 映射url
    @RequestMapping(value = "/selectPageClient", method = RequestMethod.GET)
    // 将java对象转为json格式的数据。
    @ResponseBody
    // requesetparam将请求参数绑定到控制器的方法参数上（是springmvc中接收普通参数的注解）
    public BaseModel selectPageClient(
            @RequestParam(value="curr_page",defaultValue = "1") int curr_page,
            @RequestParam(value="page_size",defaultValue = "10") int page_size,
            BaseModel baseModel,
            @RequestParam(value="address",required = false) String address,
            @RequestParam(value="client_name",required = false)String client_name,
            @RequestParam(value="type",required = false)Integer type
    ){
        // 返回服务层的分页查询方法
        return  clientService.selectPageClient(curr_page,page_size,baseModel,address,client_name,type);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "新增客户",type="新增",menu = RoleMenuEnum.NO_206)
    // 新增客户信息
    @ApiOperation(value="客户新增",httpMethod = "POST",notes="新增")
    @RequestMapping(value="/addClient", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="客户名",name="client_name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="手机号",name="phone",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="地址",name="address",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="类型",name="type",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value="拼音地址",name="py_address",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="客户名拼音",name="py_client_name",required = true,dataType = "int",paramType = "query")
    })
    public BaseModel addClient(Client client, String birthdayStr, HttpServletRequest request, BaseModel baseModel)throws Exception{
        client.setCreate_time(new Date());
        /* 获取session,判断是否登录 */
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser==null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return clientService.addClient(client,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "修改客户信息",type="修改",menu = RoleMenuEnum.NO_206)
    // 修改客户信息
    @ApiOperation(value="客户修改",httpMethod = "POST",notes="修改")
    @RequestMapping(value="/updateClient", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="客户名",name="client_name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="手机号",name="phone",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="地址",name="address",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value="类型",name="type",required = true,dataType = "int",paramType = "query")
    })
    public BaseModel updateClient(Client client,BaseModel baseModel,HttpServletRequest request)throws Exception {
        client.setUpdate_time(new Date());
        // 获取登录信息
        HttpSession session=request.getSession();
        // 记住要用User
        User currentUser=(User) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return clientService.updateClient(client,baseModel);
    }

    // 根据id查询客户信息
    @ApiOperation(value="客户查询",httpMethod = "GET",notes="查询")
    @RequestMapping(value="/selectClientById", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectClientById(@RequestParam("client_id")Integer client_id,
                                BaseModel baseModel,
                                HttpServletRequest request)throws Exception{
        return clientService.selectClientById(client_id,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "删除客户",type="删除",menu = RoleMenuEnum.NO_206)
    // 删除客户信息(修改)
    @ApiOperation(value="客户删除",httpMethod = "POST",notes="删除")
    @RequestMapping(value="/deleteClient/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel deleteClient(@PathVariable(value="ids") String ids,
                                  BaseModel baseModel,
                                  HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        // 错误示范
        // Client currentUser=(Client)session.getAttribute("currentUser");
        User currentUser = (User)session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return clientService.deleteClient(ids,baseModel);
    }
}
